package com.msmall.community.space.action.html;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.util.FilterCharsUtil;
import com.msmall.community.space.model.Words;
import com.msmall.community.space.service.WordsManager;
import com.msmall.community.space.util.SpaceConstants;

public class WordsAction extends HTMLBaseAction{
	
	private static final Log logger = LogFactory.getLog(WordsAction.class);
	
	private static final long serialVersionUID = 314346179326036685L;

	private WordsManager wordsManager;
	
	private int part = SpaceConstants.SPACE_WIDGET_PART_WORDS; //查看用户信息的哪个部分
	
	public String content;
	
	public Long receiver;
	
	public Long sender;
	
	private Integer visible;
	
	private Long wordsid;
	
	/**
	 * AJAX弹出框方式写留言
	 * @return
	 */
	public String writeWords(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			Words words = new Words();
			words.setContent(content);
			if(receiver == null){
				receiver = sender; //给自己留言
			}
			words.setReceiver(receiver);
			words.setSender(getUserid());
			words.setStatus(ConstantDB.UNREAND);
			if(visible == null){
				words.setVisible(ConstantDB.VISIBLE);
			}else{
				words.setVisible(visible);
			}
			words.setCreateTime(new Date());
			wordsManager.save(words);
			
			buffer.append(this.createResult("true"));
		}catch(Exception e){
			logger.error(e);
			buffer.append(this.createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 进入写留言页面
	 */
	public String goWriteWords(){
		return SUCCESS;
	}
	/**
	 * 删除留言
	 */
	public String remove(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			wordsManager.remove(Words.class, wordsid);
			buffer.append(this.createResult("true"));
		}catch(Exception e){
			buffer.append(this.createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	public String validateWords(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try {
			if(!getIsLogin()){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("common.authentication.anonymous")));
			}else{
				if(StringUtil.nullOrBlank(content)){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.space.words.nullorblank")));
				}else if(FilterCharsUtil.containsFilterChars(content)){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.space.words.illegal")));
				}else{
					buffer.append(this.createResult("true"));
				}
			}
		} catch (Exception e) {
			log.error(e.getStackTrace());
			buffer.append(createResult("false"));
			buffer.append(createEasyContent(getText("html.space.words.illegal")));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Long getReceiver() {
		return receiver;
	}

	public void setReceiver(Long receiver) {
		this.receiver = receiver;
	}

	public Long getSender() {
		return sender;
	}

	public void setSender(Long sender) {
		this.sender = sender;
	}

	public Integer getVisible() {
		return visible;
	}

	public void setVisible(Integer visible) {
		this.visible = visible;
	}

	public int getPart() {
		return part;
	}
	public void setPart(int part) {
		this.part = part;
	}
	public Long getWordsid() {
		return wordsid;
	}
	public void setWordsid(Long wordsid) {
		this.wordsid = wordsid;
	}
	public void setWordsManager(WordsManager wordsManager) {
		this.wordsManager = wordsManager;
	}
}
